<?php
namespace Wx\Controller;
use Think\Controller;
use Wx\Model\SendMsgModel;
class AutoController extends Controller {
    // 检测产品状态
    public function checkProduct($id){
        $shop_spellgroup_copy = M('shop_spellgroup_copy');//拼团副本
		
        $info = M('shop_spellgroup')->where("id=".$id)->find();
        // 如果产品活动时间结束，处理产品
        if($info['state']==1&&$info['endtime']<time()){
            $this->dealProduct($id);
            return 1;
        }
        // 如果产品剩余的库存不足以成团，产品下架
        $copy_num = $shop_spellgroup_copy->where('orsuccess=1 and sg_id='.$id)->sum('num');   //已成团的产品数量
//        $a = M('shop_spellgroup_copy')->getLastSql();
        $num = intval($info['num']-$copy_num);
        if($num<$info['min_num']){
            $this->dealProduct($id);
            return 1;
        }
    }//end fun checkProduct
    // 处理下架产品对应的副本及拼团订单
    public function dealProduct($id){
        $shop_spellgroup = D('shop_spellgroup');//拼团表
        $shop_spellgroup_copy = D('shop_spellgroup_copy');//拼团副本
        $shop_spellgroup_order = D('shop_spellgroup_order');
        $title = $shop_spellgroup->where('id='.$id)->getField('title');  //商品名称
        // 调用推送拼团结果模型
        $model = new SendMsgModel();
        // 调用退款类
        Vendor('Weixinpay.Weixinpay');  //导入退款类
        $refund = new \Weixinpay();     //实例化退款类
        //该产品下架
        $shop_spellgroup-> where('id='.$id)->setField('state',4);
        //第二：处理失败的副本【副本拼团标记失败】
        $copy['orsuccess'] = 2;//拼团失败
        $copy['state'] = 5;//拼团结束
        $shop_spellgroup_copy-> where('orsuccess=0 and sg_id='.$id)->save($copy);
        $getcopy = $shop_spellgroup_copy-> where('orsuccess=2 and sg_id='.$id)->select();
        //循环拼团失败的副本
        foreach($getcopy as $two){
            //第三：处理失败副本下-已付款-的订单
            $allorder = $shop_spellgroup_order->where("c_id=".$two['id'])->select();
            foreach($allorder as $three){
                if($three['state']==1){//已付款订单，退款并修改状态
                    if($three['total_fee']!=0.00){//微信支付的，退回到微信
                        // 生成退款订单号
                        $refund_sn = 'RE'.date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
                        $shop_spellgroup_order->where('id='.$three['id'])->setField('out_refund_no',$refund_sn);
                        //退款-①查询退款订单、生成退款订单号
                        $info =  M('shop_spellgroup_order')->where('id = '.$three['id'])->field('total_fee,order_sn,out_refund_no')->find();
                        $data = $refund->refund($info);     //为true表示退款成功
                        $price = $info['total_fee'];
                    }// end if
                    if($three['card_fee']!=0.00){//卡额支付的，退回到卡额,增加卡额变动记录
                        M('member')->where('id='.$three['u_id'])->setInc('credit2',$three['card_fee']);
                        changeCredit($three['u_id'],'拼团失败','credit2','+',$three['card_fee']);
                        $price = $three['card_fee'];
                    }// end if
                    $shop_spellgroup_order->where('id='.$three['id'])->setField('state',3);
                }else if($three['state']==0){//未付款的取消订单
                    $shop_spellgroup_order->where('id='.$three['id'])->setField('state',8);
                    $price = $three['total_fee'];
                }// end if
                //发送系统消息
                $s_url = '<a href="http://www.xiangtws.com/Wx/MyGrouporder/orderdetail/id/'.$three['id'].'" style="color: #ff4400">'.$title.'</a>';
                sendVoice($three['u_id'],'很抱歉，您的拼团订单'.$s_url.'活动结束前未能成团，拼团失败');
                // 推送微信消息
                $tourl = 'http://www.xiangtws.com/Wx/MyGrouporder/orderdetail/id/'.$three['id'];   //跳转链接
                $model->groupmsg($three['u_id'],'很抱歉，您的拼团订单活动结束前未能成团','拼团失败',$title,$price,$tourl);
            }// end foreach 订单处理结束
        }//end foreach
        //循环拼团失败的副本-end
        //循环拼团成功的副本
        $getcopy1 = $shop_spellgroup_copy-> where('orsuccess=1 and sg_id='.$id)->select();
//        dump($getcopy1);
        foreach ($getcopy1 as $two1){
            // 处理成功副本下-未付款的订单
            $allorder1 = $shop_spellgroup_order->where(array('c_id'=>$two1['id'],'state'=>0))->select();
            foreach ($allorder1 as $three){
                //取消订单
                $shop_spellgroup_order->where('id='.$three['id'])->setField('state',8);
                $price = $three['total_fee'];
                //发送系统消息
                $s_url = '<a href="http://www.xiangtws.com/Wx/MyGrouporder/orderdetail/id/'.$three['id'].'" style="color: #ff4400">'.$title.'</a>';
                sendVoice($three['u_id'],'很抱歉，您的拼团订单'.$s_url.'活动结束前未付款');
                // 推送微信消息
                $tourl = 'http://www.xiangtws.com/Wx/MyGrouporder/orderdetail/id/'.$three['id'];   //跳转链接
                $model->groupmsg($three['u_id'],'很抱歉，您的拼团订单活动结束前未付款','拼团失败',$title,$price,$tourl);
            }//end foreach 订单处理结束
        }//end foreach  循环拼团成功的副本-end
    }//end fun dealProduct
}//end class